package com.example.wechat.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.R;
import com.example.wechat.adapter.ImageGridAdapter;
import com.example.wechat.model.Moment;
import com.example.wechat.util.UserManager;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PostMomentActivity extends AppCompatActivity {
    private TextInputEditText contentInput;
    private RecyclerView imageGrid;
    private ImageGridAdapter imageAdapter;
    private List<String> selectedImages;
    private TextView locationText;
    private String selectedLocation;
    private ActivityResultLauncher<Intent> imagePickerLauncher;
    private ActivityResultLauncher<Intent> locationPickerLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_moment);

        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("发布朋友圈");

        // 初始化视图
        contentInput = findViewById(R.id.content);
        imageGrid = findViewById(R.id.image_grid);
        locationText = findViewById(R.id.location);
        MaterialButton addLocationBtn = findViewById(R.id.btn_add_location);
        MaterialButton postBtn = findViewById(R.id.btn_post);

        // 初始化图片选择器
        selectedImages = new ArrayList<>();
        imageAdapter = new ImageGridAdapter(selectedImages, (view, position) -> {
            // 点击图片时移除
            selectedImages.remove(position);
            imageAdapter.notifyDataSetChanged();
        });
        imageGrid.setLayoutManager(new GridLayoutManager(this, 3));
        imageGrid.setAdapter(imageAdapter);

        // 设置图片选择器启动器
        imagePickerLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                    Uri imageUri = result.getData().getData();
                    if (imageUri != null) {
                        selectedImages.add(imageUri.toString());
                        imageAdapter.notifyDataSetChanged();
                    }
                }
            }
        );

        // 设置位置选择器启动器
        locationPickerLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                    selectedLocation = result.getData().getStringExtra("location");
                    locationText.setText(selectedLocation);
                    locationText.setVisibility(android.view.View.VISIBLE);
                }
            }
        );

        // 设置添加位置按钮点击事件
        addLocationBtn.setOnClickListener(v -> {
            // TODO: 启动位置选择器
            Intent intent = new Intent(this, LocationPickerActivity.class);
            locationPickerLauncher.launch(intent);
        });

        // 设置发布按钮点击事件
        postBtn.setOnClickListener(v -> {
            String content = contentInput.getText().toString().trim();
            if (content.isEmpty() && selectedImages.isEmpty()) {
                Toast.makeText(this, "请输入内容或选择图片", Toast.LENGTH_SHORT).show();
                return;
            }

            // 创建朋友圈对象
            Moment moment = new Moment();
            moment.setId(String.valueOf(System.currentTimeMillis()));
            moment.setUserId(UserManager.getInstance().getCurrentUser().getWechatId());
            moment.setUserName(UserManager.getInstance().getCurrentUser().getNickname());
            moment.setUserAvatar(UserManager.getInstance().getCurrentUser().getAvatarUrl());
            moment.setContent(content);
            moment.setImages(selectedImages);
            moment.setTime(System.currentTimeMillis());
            moment.setLocation(selectedLocation);

            // TODO: 保存到数据库
            // MomentManager.getInstance().saveMoment(moment);

            Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show();
            finish();
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 